package tienda.zapateria.siszap.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;
import tienda.zapateria.siszap.be.BETalla;

public class DATalla extends DAOAbstract<BETalla> {

    public DATalla(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BETalla be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_talla");

            setParameterString(be.getCategoria());
            setParameterString(be.getEuropea());
            setParameterString(be.getAmericana());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BETalla be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_talla");
            setParameterInt(be.getTallaid());
            setParameterString(be.getCategoria());
            setParameterString(be.getEuropea());
            setParameterString(be.getAmericana());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BETalla be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_talla");
            setParameterInt(be.getTallaid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BETalla> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_talla");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BETalla be = new BETalla();
                be.setTallaid(getDataInt(1));
                be.setCategoria(getDataString(2));
                be.setEuropea(getDataString(3));
                be.setAmericana(getDataString(4));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BETalla> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_talla");
            setParameterString("");
            commitc();
            while (existResult()) {
                BETalla be = new BETalla();
                be.setTallaid(getDataInt(1));
                be.setCategoria(getDataString(2));
                be.setEuropea(getDataString(3));
                be.setAmericana(getDataString(4));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BETalla buscar(int i) throws Exception {
        try {
            BETalla be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_talla_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BETalla();
                be.setTallaid(getDataInt(1));
                be.setCategoria(getDataString(2));
                be.setEuropea(getDataString(3));
                be.setAmericana(getDataString(4));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

}
